#!/bin/sh
. "/usr/local/scripts/build_scripts"/util.sh
source /etc/profile
jenkins_url="http://192.168.8.246:8080/jenkins/view/"
echo cur_path=`pwd`
cur_dir=`pwd`
echo var is $1 $2 $3 $4
cd $cur_dir
echo jenkins var as follow:
echo build_num=$BUILD_NUMBER
echo build_user=$BUILD_USER
mkdir -p $BUILD_USER/$BUILD_NUMBER
cd $BUILD_USER/$BUILD_NUMBER
case $4 in
	"t_rec_sdk")
    build_module=AndroidReceiver-SDK
	;;
	"t_rec_app" )
    build_module=AndroidReceiver-APP
	;;
	"t_send_sdk")
    build_module=AndroidSender-SDK
	;;
	"t_send_app")
    build_module=AndroidSender-APP
	;;
	"phoenix")
    build_module=phoenix
	;;
	*)
	echo please input string, t_rec_sdk or t_rec_app or t_send_sdk or t_send_app ?
	exit 1
	;;
  esac
build_name="$BUILD_USER"_"$BUILD_NUMBER"_"$build_module"
echo will create and build job $build_name
curl -u admin:wangzp@360  $jenkins_url/Test_Android/job/Build_Template/config.xml -o config.xml -v 
if [ "$4" == "phoenix" ];then
    sed -i 's/build_mode/lebo_phoenix/g' config.xml
else
    sed -i 's/build_mode/bak_push_tag/g' config.xml
fi
sed -i s/build_branch/$1/g config.xml 
sed -i s/push_action/$2/g  config.xml
sed -i s/build_channel/$3/g config.xml
sed -i s/build_tvar/$4/g config.xml
sed -i s/build_code/$build_module/g config.xml
sed -i s/build_job/$build_name/g config.xml
echo run command config as follow:
cat config.xml |grep build_scripts |awk -F ">" '{print $2}'
if [ "$4" == "phoenix" ];then
    jenkins_url=$jenkins_url/phoenix
else
    jenkins_url=$jenkins_url/Test_Android
fi
echo create new jenkins $jenkins_url  job $build_name
curl -u admin:wangzp@360 -X POST $jenkins_url/createItem?name=$build_name  -d @config.xml -H "Content-Type: text/xml"
echo build jenkins job $build_name
curl -u admin:wangzp@360 -X POST $jenkins_url/job/$build_name/build
build_url="$jenkins_url/job/$build_name/1/console"
echo will trigger building $build_name and job url=$build_url


